package cn.tedu.douguo.user.service.impl;

import cn.tedu.douguo.user.mapper.CategoryMapper;
import cn.tedu.douguo.user.mapper.RecipeIngMapper;
import cn.tedu.douguo.user.mapper.RecipeMapper;
import cn.tedu.douguo.user.mapper.RecipeStepMapper;
import cn.tedu.douguo.user.pojo.dto.NewRecipeBasicDTO;
import cn.tedu.douguo.user.pojo.dto.NewRecipeDTO;
import cn.tedu.douguo.user.pojo.dto.NewRecipeIngDTO;
import cn.tedu.douguo.user.pojo.dto.NewRecipeStepDTO;
import cn.tedu.douguo.user.pojo.entity.Recipe;
import cn.tedu.douguo.user.pojo.entity.RecipeIngredients;
import cn.tedu.douguo.user.pojo.entity.RecipeStep;
import cn.tedu.douguo.user.pojo.vo.*;
import cn.tedu.douguo.user.service.CategoryService;
import cn.tedu.douguo.user.service.RecipeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;


@Service
@Slf4j
public class RecipeServiceImpl implements RecipeService {

    @Autowired
    private RecipeMapper mapper;

    @Autowired
    private RecipeStepMapper stepMapper;

    @Autowired
    private RecipeIngMapper ingMapper;

    @Override
    public void insertRecipe(NewRecipeDTO newRecipeDTO) {
        //菜单基本
        NewRecipeBasicDTO recipeBasicDTO=new NewRecipeBasicDTO();
        recipeBasicDTO.setTitle(newRecipeDTO.getTitle())
                .setImage(newRecipeDTO.getImage())
                .setAuthorId(newRecipeDTO.getAuthorId())
                .setDescription(newRecipeDTO.getDescription());
        Long id = insertRecipeBasic(recipeBasicDTO);
        //新增菜单食材
        NewRecipeIngDTO newRecipeIngDTO=new NewRecipeIngDTO();
        newRecipeIngDTO.setRecipeId(id)
                .setIngredients(newRecipeDTO.getIngredients());
        insertRecipeIng(newRecipeIngDTO);
        //新增菜单步骤
        NewRecipeStepDTO newRecipeStepDTO=new NewRecipeStepDTO();
        newRecipeStepDTO.setRecipeId(id)
                        .setStepImage(newRecipeDTO.getStepImage())
                        .setStepContent(newRecipeDTO.getStepContent());
        insertRecipeStep(newRecipeStepDTO);
    }

    private Long insertRecipeBasic(NewRecipeBasicDTO newRecipeBasicDTO) {
        Recipe recipe=new Recipe();
        BeanUtils.copyProperties(newRecipeBasicDTO,recipe);
        recipe.setPageView("0").setCollection("0")
                .setGmtCreate(LocalDateTime.now())
                .setGmtModified(LocalDateTime.now())
                .setSort(0).setKeywords(null);
        int i = mapper.insert(recipe);
        Long id = recipe.getId();
        return id;
    }


    private int insertRecipeIng(NewRecipeIngDTO newRecipeIngDTO) {
        RecipeIngredients recipeIngredients=new RecipeIngredients();
        recipeIngredients.setIngredients(newRecipeIngDTO.getIngredients())
                .setRecipeId(newRecipeIngDTO.getRecipeId())
                .setGmtCreate(new Date())
                .setGmtModified(new Date());
        int i = ingMapper.insert(recipeIngredients);
        return i;
    }


    private int insertRecipeStep(NewRecipeStepDTO newRecipeStepDTO) {

        List<String> stepImage = newRecipeStepDTO.getStepImage();
//        System.out.println(stepImage);
        List<String> stepContent = newRecipeStepDTO.getStepContent();
        String[] stepImageArr = stepImage.toArray(new String[0]);
        String[] stepContentArr = stepContent.toArray(new String[0]);
//        for (int i = 0; i < stepImageArr.length; i++) {
//            System.out.println("***********"+stepImageArr[i]);
//        }
        List<RecipeStep> recipeSteps=new LinkedList<>();
        for (int i = 0; i < stepImageArr.length; i++) {
            RecipeStep step=new RecipeStep();
            step.setImage(stepImageArr[i]);
            step.setContent(stepContentArr[i]);
            step.setRecipeId(newRecipeStepDTO.getRecipeId());
            recipeSteps.add(step);
        }
        int i=stepMapper.insertBatch(recipeSteps);
        return i;
    }



    @Override
    public List<String> getIngredientByRecipeId(Long recipeId) {
        RecipeIngredientsVO ingredient = mapper.getIngredientByRecipeId(recipeId);
        //对于食材表进行处理
        String ingredients = ingredient.getIngredients();
        String[] ingred = ingredients.split("&");
        List<String> ingredientList = Arrays.asList(ingred);
        return ingredientList;
    }

    @Override
    public List<RecipeListVO> listBySort() {
        List<RecipeListVO> list = mapper.listBySort();
//        log.warn("list******************:"+list);
        return list;

    }

    @Override
    public List<RecipeListVO> listAll() {
        List<RecipeListVO> list = mapper.listAll();
//        log.warn("list******************:"+list);
        return list;
    }

    @Override
    public List<RecipeListVO> listByKeyword(String keyword) {
        List<RecipeListVO> list = mapper.listByKeyword(keyword);
        return list;
    }

    @Override
    public RecipeStepVO getStepByRecipeId(Long recipeId) {
        RecipeStepVO step = mapper.getStepByRecipeId(recipeId);
        return step;
    }

    @Override
    public RecipeDetailsVO getDetailsByRecipeId(Long recipeId) {
        RecipeDetailsVO details = mapper.getDetailsByRecipeId(recipeId);
        return details;
    }

}
